/**
 * 帐号
 * @param {Object} main
 */
Ext.extend(Bee.module, {
    init: function(main){
        var tree = new Ext.tree.TreePanel({
            id: 'sm-accountMan-tree',
            region: 'west',
            width: 150,
            minWidth: 150,
            maxWidth: 300,
            split: true,
            useArrows: true,
            buttonStateful: false,
            autoScroll: true,
            border: false,
            loader: new Bee.tree.DWRTreeLoader({
                dwrCall: smDwr.findOrgEx,
                nodeMeta: {
                    id: 'sid',
                    text: 'cname',
                    leaf: 'isLeaf'
                }
            }),
            root: {
                nodeType: 'async',
                text: '组织机构',
                draggable: false,
                sid: '0',
                cname: '组织机构',
                leaf: false,
                expanded: true
            },
            listeners: {
                click: function(node, e){
                    if (node.isRoot) {
                    }
                    else {
                    }
                }
            }
        });
        
        var userWin = new Bee.Window({
            id: 'sm-accountMan-userWin',
            title: '人员信息',
            height: 275,
            items: new Bee.form.DWRForm({
                afterSubmit: function(){
                    userWin.hide();
                    grid.reload();
                },
                labelWidth: 85,
                dwr: {
                    load: smDwr.getUser,
                    save: smDwr.createUser,
                    update: smDwr.updateUser
                },
                items: [{
                    fieldLabel: '主键',
                    name: 'sid',
                    xtype: 'hidden'
                }, {
                    fieldLabel: '帐号',
                    name: 'account',
                    allowBlank: false
                }, {
                    fieldLabel: '密码',
                    name: 'pwd',
                    xtype: 'hidden'
                }, {
                    fieldLabel: '姓名',
                    name: 'cname',
                    allowBlank: false
                }, {
                    fieldLabel: '说明',
                    name: 'descriptions',
                    maxLength: 200,
                    xtype: 'textarea'
                }, {
                    id: 'sm-accountMan-userWin_orgSid',
                    fieldLabel: '所属处室',
                    hiddenName: 'orgSid',
                    xtype: 'combotree',
                    rootVisible: false,
                    displayField: 'cname',
                    valueField: 'sid',
                    dwrCall: smDwr.findOrg1,
                    rootId: '0',
                    rootText: '重庆市国资委'
                }, {
                    fieldLabel: '排序号',
                    name: 'orderBy',
                    xtype: 'numberfield',
                    maxLength: 3,
                    allowDecimals: false,
                    allowNegative: false,
                    allowBlank: false
                }, {
                    fieldLabel: '类型',
                    name: 'types',
                    value: '1',
                    xtype: 'hidden'
                }, {
                    fieldLabel: '状态',
                    name: 'status',
                    value: '1',
                    xtype: 'hidden'
                }]
            }),
            listeners: {
                'show': {
                    fn: function(){
                        if (!Ext.getCmp('sm-accountMan-userWin_orgSid').tree.rendered) {
                            var treeCombo = Ext.getCmp('sm-accountMan-userWin_orgSid');
                            treeCombo.focus();
                            treeCombo.expand();
                            treeCombo.collapse();
                        }
                    },
                    single: true
                }
            }
        });
        
        var roleGrid = new Bee.grid.ListGrid({
            id: 'sm-accountMan-roleGrid',
            border: false,
            initload: true,
            firstColumn: 'checkbox',
            filter: {
                status: 1
            },
            dwr: {
                call: smDwr.findRole
            },
            metas: [{
                id: 'sid',
                field: 'sid',
                type: 'int',
                hidden: true
            }, {
                field: 'cname',
                desc: '角色名称'
            }, {
                field: 'descriptions',
                desc: '角色说明'
            }]
        });
        
        var roleWin = new Bee.Window({
            id: 'sm-accountMan-roleWin',
            title: '人员关联角色',
            width: 400,
            height: 300,
            items: roleGrid,
            save: function(){
                Ext.Msg.confirm('提交', '确认要提交表单里的内容吗？', function(para){
                    if (para == 'yes') {
                        var record = roleGrid.getSelectionModel().getSelections();
                        var ids = [];
                        for (var i = 0; i < record.length; i++) {
                            ids.push(record[i].get('sid'));
                        }
                        roleWin.getEl().mask('保存中，请稍候...');
                        smDwr.saveRoleByUser(grid.getSelectedRow().sid, ids, function(){
                            roleWin.getEl().unmask();
                            roleWin.hide();
                            Bee.msg('关联角色成功！');
                        });
                    }
                });
            },
            listeners: {
                'show': function(o){
                    smDwr.findRoleByUser(grid.getSelectedRow().sid, function(out){
                        roleGrid.selecteds = out;
                        roleGrid.reload();
                    });
                }
            }
        });
        
        var picWin = new Bee.Window({
            id: 'sm-accountMan-picWin',
            title: '照片上传窗口',
            autoHeight: true,
            save: function(){
                var form = Ext.getCmp('sm-accountMan-picWin_formpanel');
                if (form.getForm().isValid()) {
                    picWin.getEl().mask('上传中，请稍候...');
                    form.getForm().submit({
                        url: 'pic.do',
                        waitTitle: '上传进度',
                        waitMsg: '上传中，请稍候...',
                        success: function(form, action){
                            picWin.getEl().unmask();
                            Ext.Msg.alert('提示', '照片上传成功！');
                            grid.reload();
                            picWin.hide();
                        },
                        failure: function(form, action){
                            picWin.getEl().unmask();
                            Ext.Msg.alert('提示', '照片上传失败！');
                            grid.reload();
                            picWin.hide();
                        }
                    });
                }
            },
            items: new Ext.form.FormPanel({
                id: 'sm-accountMan-picWin_formpanel',
                fileUpload: true,
                border: false,
                labelWidth: 60,
                autoHeight: true,
                bodyStyle: 'padding:5px;',
                defaults: {
                    anchor: '90%',
                    msgTarget: 'side',
                    labelSeparator: ''
                },
                items: [{
                    xtype: 'fileuploadfield',
                    emptyText: '选择要上传的照片',
                    fieldLabel: '选择照片',
                    name: 'picUrl',
                    buttonText: '',
                    allowBlank: false,
                    buttonCfg: {
                        iconCls: 'btn_upload'
                    }
                }, {
                    id: 'sm-accountMan-picWin_formpanel_userSid',
                    fieldLabel: 'userSid',
                    name: 'userSid',
                    xtype: 'hidden'
                }]
            })
        });
        
        var grid = new Bee.grid.PageGrid({
            id: 'sm-accountMan-grid',
            region: 'center',
            initload: false,
            border: false,
            filter: {
                orgSid: 0,
                status: 1
            },
            sort: {
                orderBy: 'desc'
            },
            dwr: {
                call: smDwr.pageUser
            },
            metas: [{
                id: 'sid',
                field: 'sid',
                type: 'int',
                hidden: true
            }, {
                field: 'account',
                desc: '帐号'
            }, {
                field: 'cname',
                desc: '姓名'
            }, {
                field: 'orderBy',
                desc: '排序号'
            }],
            tbar: [{
                id: 'sm-accountMan-grid_createBtn',
                text: '新增',
                disabled: true,
                iconCls: 'btn_insert',
                handler: function(){
                    userWin.show();
                    Ext.getCmp('sm-accountMan-userWin_orgSid').setValue(grid.filter.orgSid);
                }
            }, {
                id: 'sm-accountMan-grid_updateBtn',
                text: '修改',
                disabled: true,
                iconCls: 'btn_modify',
                handler: function(){
                    var record = grid.getSelectedRow();
                    if (record) {
                        userWin.load(record.sid);
                    }
                    else {
                        Bee.msg('请选择要修改的数据！');
                        return false;
                    }
                }
            }, {
                id: 'sm-accountMan-grid_reBtn',
                text: '人员关联角色',
                disabled: true,
                iconCls: 'btn_config',
                handler: function(){
                    var record = grid.getSelectedRow();
                    if (record) {
                        roleWin.show();
                    }
                    else {
                        Bee.msg('请选择要修改的数据！');
                        return false;
                    }
                }
            }, {
                id: 'sm-accountMan-grid_resetBtn',
                text: '重置密码',
                disabled: true,
                iconCls: 'btn_lock',
                handler: function(){
                    var record = grid.getSelectedRow();
                    if (record) {
                        Ext.Msg.confirm('提示', '确认重置密码吗?', function(param){
                            if (param == 'yes') {
                                smDwr.doResetPwd(grid.getSelectedRow().sid, function(){
                                    Bee.msg('重置密码成功!');
                                });
                            }
                        });
                    }
                    else {
                        Bee.msg('请选择要修改的数据！');
                        return false;
                    }
                }
            }, {
                id: 'sm-accountMan-grid_deleteBtn',
                text: '失效',
                disabled: true,
                iconCls: 'btn_delete',
                handler: function(){
                    var record = grid.getSelectedRow();
                    if (record) {
                        Ext.Msg.confirm('提示', '确认失效吗?', function(param){
                            if (param == 'yes') {
                                smDwr.deleteUser(grid.getSelectedRow().sid, function(){
                                    Bee.msg('失效成功!');
                                    grid.reload();
                                });
                            }
                        });
                    }
                    else {
                        Bee.msg('请选择要修改的数据！');
                        return false;
                    }
                }
            }, {
                id: 'sm-accountMan-grid_addPicBtn',
                text: '上传照片',
                disabled: true,
                iconCls: 'btn_insert',
                handler: function(){
                    var record = grid.getSelectedRow();
                    if (record) {
                        picWin.show();
                        var userSid = Ext.getCmp('sm-accountMan-picWin_formpanel_userSid');
                        userSid.setValue(grid.getSelectedRow().sid);
                    }
                    else {
                        Bee.msg('请选择要上传照片的领导！');
                        return false;
                    }
                }
            }]
        });
        
        var panel = new Ext.Panel({
            id: 'sm-accountMan-panel',
            layout: 'border',
            border: false,
            items: [tree, grid]
        });
        
        userWin.show();
        userWin.hide();
        
        this.main.associatedComp = [userWin, roleWin, picWin];
        this.main.add(panel);
        this.main.doLayout();
    }
});
